String.prototype.startsWith = function(text) {
	if (typeof this === "undefined")
		return false;
	return this.substr(0, text.length) === text;
};

String.prototype.leftTrim = function(index) {
	if (typeof this == "undefined")
		return "";

	return this.substr(index, this.length);
};

function handleSearch(repoUrl) {
	var searchText = $('#searchform > #searchtext').val();
	$.get("/codesphereweb/resources/svnlog/search/" + searchText + "?repo="
			+ repoUrl, function(json) {
		
		$('#results').children().remove().end();
		
		$('#searchResultsTemplate').tmpl({
			"searchResults" : json,
			"searchText" : searchText
		}).appendTo('#results');

		
		$("#searchViewLink").click();
		scrollToTop();
	});
}

function getTr(linenumber, tag, originalCodeline, revisedCodeline) {
	if (tag === "EQUAL")
		return getTrHtml(linenumber, "none", originalCodeline, "none",
				revisedCodeline);

	if (tag == "CHANGE")
		return getTrHtml(linenumber, "old", originalCodeline, "new",
				revisedCodeline);

	if (tag === "INSERT")
		return getTrHtml(linenumber, "noline", originalCodeline, "new",
				revisedCodeline);

	if (tag === "DELETE")
		return getTrHtml(linenumber, "old", originalCodeline, "noline",
				revisedCodeline);
}

function getTrHtml(linenumber, originalstyle, originalcode, revisedstyle,
		revisedcode) {
	return "<tr><th>" + linenumber + "</th><td class='" + originalstyle + "'><pre>"
			+ getPureSource(originalcode) + "</pre></td><th>" + linenumber
			+ "</th><td class='" + revisedstyle + "'><pre>"
			+ getPureSource(revisedcode) + "</pre></td></tr>";
}

function getPureSource(codeline) {
	if (typeof codeline === "undefined")
		return "";

	if (codeline.startsWith("changed"))
		return codeline.substr("changed".length + 1, codeline.length - 1);
	else
		return codeline;
}

function scrollToTop() {
	scrollTo(0, 0);
}

function handlePaginationClick(new_page_index, pagination_container) {
	getSvnLog(new_page_index + 1);
	scrollToTop();
	return false;
}

function generateChangeSetHtml(item) {
	var changeSetHtml = '';
	$.each(item.revisionChanges, function() {
		var changes = this;

		var filesplits = changes.path.split('/');
		var filename = filesplits[filesplits.length - 1];

		changeSetHtml += '<li>' + changes.changeType + ' - '
				+ '<a id="changesetfile" href="#' + filename + '">'
				+ item.revision + ':' + changes.path
				+ '</a> - <a id="difflink" href="#' + changes.path
				+ '">(diff)</a></li>';
	});

	return changeSetHtml;
}

function showSourceView(data) {
	var splitString = data.split(':');
	$.get('/codesphereweb/resources/sourceview/' + splitString[0] + '?file='
			+ splitString[1], function(data) {

		var file_details = '<h3>' + splitString[1] + '</h3>';

		$('#filename').html(file_details);

		$('pre.sourcecode').text(data.sourceCode);

		$('pre.sourcecode').snippet(data.format, {
			style : "emacs",
			transparent : true,
			showNum : false
		});

		$("#sourceViewLink").click();
		scrollToTop();
	});
}

function showDiffSelections(difflink) {
	var path = difflink.toString().split('#')[1];
	$('#svnlog #divdiffform').remove();

	$.get('/codesphereweb/resources/revisions?path=' + path, function(json) {
		$('#diffformtemplate').tmpl({
			"revisions" : json,
			"path" : path
		}).insertAfter(difflink);

		$('#diffform').submit(
				function() {
					var oldRevision = $('#diffform > #old').val();
					var newRevision = $('#diffform > #new').val();
					var path = $('#diffform > #path').val();

					$('#diffview > table').remove();

					$.get('/codesphereweb/resources/diffview/' + newRevision
							+ '?old=' + oldRevision + '&path=' + path,
							function(data) {

								$('#difftemplate').tmpl(data).appendTo(
										'#diffview');
								$("#diffViewLink").click();
								scrollToTop();
							});

					return false;
				});
	});

}

function generateRevisionEntry(item, changeSetHtml) {
	$('#entries').append(
			'<div id="logentry"><p id="author" class="author">' + item.author
					+ '<span class="date"> (Revision: ' + item.revision
					+ ') at ' + item.date
					+ '</span></p><p id="message" class="message">'
					+ item.message + '</p><div id="changeset"><ul>'
					+ changeSetHtml + '</ul></div></div>');
}

function getSvnLog(page_number) {
	$.get('/codesphereweb/resources/svnlog/' + page_number + "?repo="
			+ $.url.param("repo"), function(json) {

		$('#entries').children().remove().end();

		$.each(json, function() {
			var item = this;

			changeSetHtml = generateChangeSetHtml(item);
			generateRevisionEntry(item, changeSetHtml);
		});

		$("#changeset > ul > li > a[id=changesetfile]").click(function(event) {
			showSourceView(this.innerHTML);
		});

		$("#changeset > ul > li > a[id=difflink]").click(function(event) {
			showDiffSelections(this);
		});
	});
};
